package com.servlet.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import DB.DBManager;

import com.servlet.vo.Payment;

public class PaymentDao {

	private Connection con;
	private Statement statement;
	private ResultSet resultSet;
	private boolean flag = false;
	private StringBuffer sql = new StringBuffer();
	
	public PaymentDao() throws SQLException{
		con = DBManager.getConn();
		statement = con.createStatement();
	}
	public void close(){
		
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != statement){
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean save(Payment obj) {
		
		sql.append("INSERT INTO payment(ADDTIME,deleteStatus,paymentName) VALUES( NOW(),FALSE,'");
		sql.append(obj.getPaymentName() + "')");
		
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = true;
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean update(Payment obj){
		
		try {
			sql.append("UPDATE payment(");
			if(obj.getPaymentName() != null){
				sql.append("paymentName,");
			}
			if(sql.toString().endsWith(",")){
				sql.deleteCharAt(sql.toString().length()-1);
				sql.append(") values(");
				if(obj.getPaymentName() != null){
					sql.append("'" + obj.getPaymentName() + "',");
				}
				if(sql.toString().endsWith(",")){
					sql.deleteCharAt(sql.toString().length()-1);
					sql.append(")");
					
					try {
						flag = statement.execute(sql.toString());
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		
		sql.append("UPDATE payment SET deleteStatus = TRUE WHERE id = '" + id + "'");
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public List<Payment> getList(){
		
		sql.append("SELECT id,paymentName FROM payment WHERE deleteStatus = FALSE ");
		List<Payment> objs = new ArrayList<Payment>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			Payment payment = null;
			while(resultSet.next()){
			
				payment = new Payment();
				payment.setId(resultSet.getLong("id"));
				payment.setPaymentName(resultSet.getString("paymentName"));
				objs.add(payment);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
}
